<?php

namespace backend\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "country".
 *
 * @property integer $id
 * @property string $title
 * @property integer $pid
 * @property integer $state
 * @property integer $order
 */
class Country extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'country';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pid', 'state', 'order'], 'integer'],
            [['order'], 'required'],
            [['title'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'pid' => 'Pid',
            'state' => 'State',
            'order' => 'Order',
        ];
    }

    /**
     * @inheritdoc
     * @return CountryQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new CountryQuery(get_called_class());
    }

    public static function localCity($where)
    {
        return ArrayHelper::map(Country::find()->where($where)->orderBy('order ASC')->all(), 'id', 'title');
    }


    public static function getCityList($pid=0)
    {
        if($pid==0){
            $model = Country::find()->where("`pid`=0 AND `id` < 32 AND `id` !=26")->orderBy('order ASC')->all();
        }else{
            $model = Country::find()->where("`pid`=$pid ")->orderBy('order ASC')->all();
        }

        return ArrayHelper::map($model, 'id', 'title');
    }
    
    public function getProvinces($where = 'pid=0')
    {
        $arr_province = Country::find()->select(['id', 'title'])->where($where)->orderBy('order')->indexBy('id')->asArray()->all();
        unset($arr_province[32], $arr_province[33], $arr_province[34]);
        return $arr_province = \yii\helpers\ArrayHelper::map($arr_province, 'id', 'title');
    }

    public function getProvincesForDropdown($where = "pid=0")
    {
        $arr_province = Country::find()->select("id,title AS name")->where($where)->orderBy('order')->indexBy('id')->asArray()->all();
        unset($arr_province[32], $arr_province[33], $arr_province[34]);
        return $arr_province;
    }

}
